<?php

namespace App\Console\Commands\Task;


use App\Dep\ProductDep;
use App\Dep\TaskDep;
use App\Enum\TaskEnum;
use App\Lib\AigcSdk;
use Illuminate\Console\Command;


class ChatCmd extends Command
{

    public $signature = 'task:chat';

    public function handle()
    {
        $dep = new TaskDep();
        $resTask = $dep->getWaitTask();
        if ($resTask->isEmpty()) {
            $this->log('have no wait tasks');
            return false;
        }

        $productDep = new ProductDep();
        $sdk = new AigcSdk();

        $resTask->each(function ($item) use ($productDep, $sdk, $dep) {
            $resProduct = $productDep->first($item->product_id);
            $json = $this->getJson();
            $resChat = $sdk->chat("帮我生成产品介绍的文案,返回的文案放到我提供的json结构里，json结构如下：{$json}", "产品名称是{$resProduct->name},产品卖点有{$resProduct->point}");
            $origin = $resChat['output']['choices'][0]['message']['content'];
            $this->log("chat res {$origin}");
            $content = str_replace(['```'], '', $origin);
            $contentArr = json_decode($content, true);

            if (!$this->checkContent($contentArr)) {
                $data = [
                    'status' => TaskEnum::STATUS_MODEL_ERROR
                ];
                $dep->edit($item->id, $data);
                return true;
            }

            $perRow = TaskEnum::PER_SECOND * $item->row_second;
            $result = [
                [
                    'type' => '开场白',
                    'content' => mb_substr($contentArr['open_remarks'],0,$perRow) ?? "",
                    'timeline' => "",
                    'count' => mb_strlen(mb_substr($contentArr['open_remarks'],0,$perRow)),
                    'duration' => 0,
                    'product_resource' => '',
                    'product_resource_second' => 0,
                    'color_text' => '',
                    'color_text_second' => 0,
                    'sound' => '',
                    'special' => ''
                ],
                [
                    'type' => '开场白',
                    'content' => mb_substr($contentArr['open_remarks'],$perRow - 1,$perRow) ?? "",
                    'timeline' => "",
                    'count' => mb_strlen(mb_substr($contentArr['open_remarks'],$perRow - 1,$perRow)),
                    'duration' => 0,
                    'product_resource' => '',
                    'product_resource_second' => 0,
                    'color_text' => '',
                    'color_text_second' => 0,
                    'sound' => '',
                    'special' => ''
                ],
                [
                    'type' => '开场白',
                    'content' => mb_substr($contentArr['open_remarks'],2 * $perRow - 1,$perRow) ?? "",
                    'timeline' => "",
                    'count' => mb_strlen(mb_substr($contentArr['open_remarks'],2 * $perRow - 1,$perRow)),
                    'duration' => 0,
                    'product_resource' => '',
                    'product_resource_second' => 0,
                    'color_text' => '',
                    'color_text_second' => 0,
                    'sound' => '',
                    'special' => ''
                ],
                [
                    'type' => '段落一',
                    'content' => mb_substr($contentArr['section_1'],0,$perRow) ?? "",
                    'timeline' => "",
                    'count' => mb_strlen(mb_substr($contentArr['section_1'],0,$perRow)),
                    'duration' => 0,
                    'product_resource' => '',
                    'product_resource_second' => 0,
                    'color_text' => $contentArr['signature_1'],
                    'color_text_second' => 0,
                    'sound' => '',
                    'special' => ''
                ],
                [
                    'type' => '段落一',
                    'content' => mb_substr($contentArr['section_1'],$perRow - 1,$perRow) ?? "",
                    'timeline' => "",
                    'count' => mb_strlen(mb_substr($contentArr['section_1'],$perRow - 1,$perRow)),
                    'duration' => 0,
                    'product_resource' => '',
                    'product_resource_second' => 0,
                    'color_text' => "",
                    'color_text_second' => 0,
                    'sound' => '',
                    'special' => ''
                ],
                [
                    'type' => '段落一',
                    'content' => mb_substr($contentArr['section_1'],2 * $perRow - 1,$perRow) ?? "",
                    'timeline' => "",
                    'count' => mb_strlen(mb_substr($contentArr['section_1'],2 * $perRow - 1,$perRow)),
                    'duration' => 0,
                    'product_resource' => '',
                    'product_resource_second' => 0,
                    'color_text' =>  "",
                    'color_text_second' => 0,
                    'sound' => '',
                    'special' => ''
                ],
                [
                    'type' => '段落二',
                    'content' => mb_substr($contentArr['section_2'],0,$perRow) ?? "",
                    'timeline' => "",
                    'count' => mb_strlen(mb_substr($contentArr['section_2'],0,$perRow)),
                    'duration' => 0,
                    'product_resource' => '',
                    'product_resource_second' => 0,
                    'color_text' => $contentArr['signature_2'],
                    'color_text_second' => 0,
                    'sound' => '',
                    'special' => ''
                ],
                [
                    'type' => '段落二',
                    'content' => mb_substr($contentArr['section_2'],$perRow - 1,$perRow) ?? "",
                    'timeline' => "",
                    'count' => mb_strlen(mb_substr($contentArr['section_2'],$perRow - 1,$perRow)),
                    'duration' => 0,
                    'product_resource' => '',
                    'product_resource_second' => 0,
                    'color_text' => "",
                    'color_text_second' => 0,
                    'sound' => '',
                    'special' => ''
                ],
                [
                    'type' => '段落二',
                    'content' => mb_substr($contentArr['section_2'],2 * $perRow - 1,$perRow) ?? "",
                    'timeline' => "",
                    'count' => mb_strlen(mb_substr($contentArr['section_2'],2 * $perRow - 1,$perRow)),
                    'duration' => 0,
                    'product_resource' => '',
                    'product_resource_second' => 0,
                    'color_text' =>  "",
                    'color_text_second' => 0,
                    'sound' => '',
                    'special' => ''
                ],
                [
                    'type' => '段落三',
                    'content' => mb_substr($contentArr['section_3'],0,$perRow) ?? "",
                    'timeline' => "",
                    'count' => mb_strlen(mb_substr($contentArr['section_3'],0,$perRow)),
                    'duration' => 0,
                    'product_resource' => '',
                    'product_resource_second' => 0,
                    'color_text' => $contentArr['signature_3'],
                    'color_text_second' => 0,
                    'sound' => '',
                    'special' => ''
                ],
                [
                    'type' => '段落三',
                    'content' => mb_substr($contentArr['section_3'],$perRow - 1,$perRow) ?? "",
                    'timeline' => "",
                    'count' => mb_strlen(mb_substr($contentArr['section_3'],$perRow - 1,$perRow)),
                    'duration' => 0,
                    'product_resource' => '',
                    'product_resource_second' => 0,
                    'color_text' => "",
                    'color_text_second' => 0,
                    'sound' => '',
                    'special' => ''
                ],
                [
                    'type' => '段落三',
                    'content' => mb_substr($contentArr['section_3'],2 * $perRow - 1,$perRow) ?? "",
                    'timeline' => "",
                    'count' => mb_strlen(mb_substr($contentArr['section_3'],2 * $perRow - 1,$perRow)),
                    'duration' => 0,
                    'product_resource' => '',
                    'product_resource_second' => 0,
                    'color_text' =>  "",
                    'color_text_second' => 0,
                    'sound' => '',
                    'special' => ''
                ],
                [
                    'type' => '结束语',
                    'content' => mb_substr($contentArr['complimentary_close'],0,$perRow) ?? "",
                    'timeline' => "",
                    'count' => mb_strlen(mb_substr($contentArr['complimentary_close'],0,$perRow)),
                    'duration' => 0,
                    'product_resource' => '',
                    'product_resource_second' => 0,
                    'color_text' => "",
                    'color_text_second' => 0,
                    'sound' => '',
                    'special' => ''
                ],
                [
                    'type' => '结束语',
                    'content' => mb_substr($contentArr['complimentary_close'],$perRow - 1,$perRow) ?? "",
                    'timeline' => "",
                    'count' => mb_strlen(mb_substr($contentArr['complimentary_close'],$perRow - 1,$perRow)),
                    'duration' => 0,
                    'product_resource' => '',
                    'product_resource_second' => 0,
                    'color_text' => "",
                    'color_text_second' => 0,
                    'sound' => '',
                    'special' => ''
                ],
                [
                    'type' => '结束语',
                    'content' => mb_substr($contentArr['complimentary_close'],2 * $perRow - 1,$perRow) ?? "",
                    'timeline' => "",
                    'count' => mb_strlen(mb_substr($contentArr['complimentary_close'],2 * $perRow - 1,$perRow)),
                    'duration' => 0,
                    'product_resource' => '',
                    'product_resource_second' => 0,
                    'color_text' =>  "",
                    'color_text_second' => 0,
                    'sound' => '',
                    'special' => ''
                ],
            ];

            $data = [
                'status' => TaskEnum::STATUS_MODEL_SUCCESS,
                'model_origin' => $content,
                'result' => json_encode($result)
            ];
            $dep->edit($item->id, $data);
        });
    }

    private function log($msg, $context = [])
    {
        $this->info("task chat log: {$msg} " . json_encode($context));
        log_daily("task-chat")->info($msg, $context);
    }

    private function getJson()
    {
        $res = file_get_contents(config_path('task.json'));
        return $res;
    }

    private function checkContent($contentArr)
    {
        $needArr = ['title', 'open_remarks', 'section_1', 'signature_1', 'section_2', 'signature_2', 'complimentary_close'];
        foreach ($needArr as $need) {
            if (!isset($contentArr[$need])) {
                return false;
            }
        }
        return true;
    }
}
